package com.mall.config;

import com.mall.interceptor.SysInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebAppConfigurer implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("http://localhost:8081") // 允许跨域请求的域名
                .allowCredentials(true)
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE","OPTIONS")
                .maxAge(3600);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/image/swiper/**")
                .addResourceLocations("file:E:\\workspace\\mall-image\\swiperImgs\\");
        registry.addResourceHandler("/image/bigType/**")
                .addResourceLocations("file:E:\\workspace\\mall-image\\bigTypeImgs\\");
        registry.addResourceHandler("/image/product/**")
                .addResourceLocations("file:E:\\workspace\\mall-image\\productImgs\\");
        registry.addResourceHandler("/image/productSwiper/**")
                .addResourceLocations("file:E:\\workspace\\mall-image\\productSwiperImgs\\");
        registry.addResourceHandler("/image/productIntroImgs/**")
                .addResourceLocations("file:E:\\workspace\\mall-image\\productIntroImgs\\");
        registry.addResourceHandler("/image/productParaImgs/**")
                .addResourceLocations("file:E:\\workspace\\mall-image\\productParaImgs\\");
    }

    @Bean
    public SysInterceptor sysInterceptor() {
        return new SysInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        String[] patterns=new String[]{"/adminLogin","/product/**","/bigType/**","/user/wxlogin","/weixinpay/**"};
        registry.addInterceptor(sysInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns(patterns);
    }
}
